package com.cat.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;

import com.cat.bean.common.PageBean;

@SuppressWarnings("serial")
public class TestAction extends BaseAction {
	
	private ProcessEngine processEngine;

	public void setProcessEngine(ProcessEngine processEngine) {
		this.processEngine = processEngine;
	}

	public ProcessEngine getProcessEngine() {
		return processEngine;
	}

	private List<PageBean> comments = new ArrayList<PageBean>();
	// 没getter and setter方法的属性不会被串行化到JSON
	@SuppressWarnings("unused")
	private String title;

	private File flowFile;

	public File getFlowFile() {
		return flowFile;
	}

	public void setFlowFile(File flowFile) {
		this.flowFile = flowFile;
	}

	private String content;

	public String loadInfo1() {
		title = "222";
		content = "你是木头人，哈哈。";
		loadComments();
		return SUCCESS;
	}

	public String loadInfo2() {
		title = "111";
		content = "你是木头人，哈哈。";
		loadComments();
		return SUCCESS;
	}

	/**
	 * 加载留言信息
	 */
	private void loadComments() {
		PageBean com1 = new PageBean();
		com1.setCurrentPage("2");
		com1.setCurrentRows("1");
		com1.setPageSize("2");
		PageBean com2 = new PageBean();
		com2.setCurrentPage("2");
		com2.setCurrentRows("1");
		com2.setPageSize("2");
		comments.add(com1);
		comments.add(com2);
	}

	public List<PageBean> getComments() {
		return comments;
	}

	public void setComments(List<PageBean> comments) {
		this.comments = comments;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * 测试是否整合成功
	 * 
	 * @throws IOException
	 */
	public String testJbpm() throws IOException {
		System.out.println("整合成功");
		System.out.println("processEngine: " + processEngine);

		return SUCCESS;
	}

	public String addFlow() throws IOException {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		File flowFile = new File("D:\\学习\\jbpm\\jbpmssh2\\src\\leave2.zip");
		InputStream is = new FileInputStream(flowFile);
		ZipInputStream zis = new ZipInputStream(is);

		repositoryService.createDeployment().addResourcesFromZipInputStream(zis).deploy();
		zis.close();
		is.close();
		System.out.println("发布流程成功<br />");
		return ajaxText("s");
	}
}
